package cn.biplam.back.operator.mdManage;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.mdManage.TypeCode.DbSourceCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 数据源信息表数据操作类
 * @author XiaoRuxing 2009-5-6下午07:46:00
 * @version 1.0
 */
public class DbSource extends DataOperator{

	private static final String tName="dbsource";

	/**
	 * 创建数据源信息数据操作类
	 * @param userId 用户ID
	 * @throws ErrorMessage
	 */
	public DbSource() throws ErrorMessage {
		super(tName);
	}

	/**
	 * 实现类的交换器，根据交换代码执行相应的方法生成并执行SQL语句，返回执行结果
	 * @return 交换代码对应Sql的执行结果
	 * @throws ErrorMessage
	 */
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql=new ArrayList<String>();
		if(opId.equals(DbSourceCode.Select_All)){
			this.selectAllDbSource(sql,filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(DbSourceCode.Select_By_ID)){
			this.selecttDbSourceByID(sql,filter);
			return this.doing(sql,Operator_Type.Select);	
		}else if(opId.equals(DbSourceCode.Insert)){
			this.insert(sql,filter);
			return this.doing(sql,Operator_Type.DML);
		}else if(opId.equals(DbSourceCode.Select_By_Type)){
			this.selectByModelType(sql, filter);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(DbSourceCode.Update)){
			this.update(sql,filter);
			return this.doing(sql,Operator_Type.DML);
		}else if(opId.equals(DbSourceCode.Delete_By_ID)){
			this.deleteByID(sql,filter);
			return this.doing(sql,Operator_Type.DML);
		}else{
			throw new ErrorMessage("Data Operator id not found:"+ opId+" !");
		}
	}

	/** 查询所有数据源信息 */
	public void selectAllDbSource(ArrayList<String> sql,DataFilter filter){
		StringBuilder sp=new StringBuilder();
		sp.append("select dsid, dsname, dsdesc, type, dssid, dstype,       ");
		sp.append("       username, password, db_type_id, ip, port, main_db    ");
		sp.append("   from  dbsource t  order by t.type,t.dsname     ");
		sql.add(sp.toString());
	}

	/** 根据数据源ID查询单个数据源信息 */
	public void selecttDbSourceByID(ArrayList<String> sql,DataFilter filter){
		StringBuilder sp=new StringBuilder();
		sp.append("select dsid, dsname, dsdesc, type, dssid,dstype,           ");
		sp.append("       username, password, db_type_id, ip, port, main_db    ");
		sp.append("   from  dbsource t                                   ");
		sp.append(" where dsid='");
		sp.append(filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_ID));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据模型类型查询数据源  
	 */
	public void selectByModelType(ArrayList<String> sql,DataFilter filter){
		StringBuilder sp=new StringBuilder();
		sp.append("select dsid, dsname, dsdesc, type, dssid, dstype,              ");
		sp.append("       username, password, db_type_id, ip, port, main_db    ");
		sp.append("   from  dbsource t                                   ");
		sp.append(" where type='");
		sp.append(filter.getValue(MdStruc.DbSourStruc.MODEL_TYPE));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 * 创建数据源
	 * @param sql
	 * @throws ErrorMessage
	 */
	public void insert(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		Insert insert = new Insert(this.tableName);
		String dsid = this.generatorRowID();
		insert.putStringFieldDenyNull(MdStruc.DbSourStruc.DB_SOURCE_ID, dsid);
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_SOURCE_NAME, filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_SOURCE_DESC, filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_DESC));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_SOURCE_SID, filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_SID));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.IP, filter.getValue(MdStruc.DbSourStruc.IP));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.PORT, filter.getValue(MdStruc.DbSourStruc.PORT));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.USRE_NAME, filter.getValue(MdStruc.DbSourStruc.USRE_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.PASSWORD, filter.getValue(MdStruc.DbSourStruc.PASSWORD));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_TYPE_ID, filter.getValue(MdStruc.DbSourStruc.DB_TYPE_ID));
		insert.putStringFieldIgnoreNull(MdStruc.DbSourStruc.MAIN_DB, filter.getValue(MdStruc.DbSourStruc.MAIN_DB));
		insert.putStringFieldDenyNull(MdStruc.DbSourStruc.MODEL_TYPE, filter.getValue(MdStruc.DbSourStruc.MODEL_TYPE));
		insert.putStringFieldDenyNull(MdStruc.DbSourStruc.DSTYPE, filter.getValue(MdStruc.DbSourStruc.DSTYPE));
		sql.add(insert.getSimpleSql());
	}
	
	/**
	 * 修改数据源
	 * @param sql
	 * @throws ErrorMessage
	 */
	public void update(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		Update update = new Update(this.tableName);
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_SOURCE_NAME, filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_NAME));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_SOURCE_DESC, filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_DESC));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_SOURCE_SID, filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_SID));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.IP, filter.getValue(MdStruc.DbSourStruc.IP));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.PORT, filter.getValue(MdStruc.DbSourStruc.PORT));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.USRE_NAME, filter.getValue(MdStruc.DbSourStruc.USRE_NAME));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.PASSWORD, filter.getValue(MdStruc.DbSourStruc.PASSWORD));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.DB_TYPE_ID, filter.getValue(MdStruc.DbSourStruc.DB_TYPE_ID));
		update.putStringFieldIgnoreNull(MdStruc.DbSourStruc.MAIN_DB, filter.getValue(MdStruc.DbSourStruc.MAIN_DB));
		update.putStringFieldDenyNull(MdStruc.DbSourStruc.MODEL_TYPE, filter.getValue(MdStruc.DbSourStruc.MODEL_TYPE));
		update.putStringFieldDenyNull(MdStruc.DbSourStruc.DSTYPE, filter.getValue(MdStruc.DbSourStruc.DSTYPE));
		update.setWhere(MdStruc.DbSourStruc.DB_SOURCE_ID+"='"+filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_ID)+"'");
		sql.add(update.getSimpleSql());
	}
	
	/**
	 * 删除数据源
	 * @param sql
	 * @throws ErrorMessage
	 */
	public void deleteByID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		Delete delete = new Delete(this.tableName);
		delete.setWhere(MdStruc.DbSourStruc.DB_SOURCE_ID+"='"+filter.getValue(MdStruc.DbSourStruc.DB_SOURCE_ID)+"'");
		sql.add(delete.getSimpleSql());
	}
}
